import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Test;
import org.w3c.dom.Document;

import ru.ksu.ktk.diploma.sdi.dao.ResourcesDao;
import ru.ksu.ktk.diploma.sdi.mapping.Correspondence;
import ru.ksu.ktk.diploma.sdi.mapping.MappingGenerator;
import ru.ksu.ktk.diploma.sdi.mapping.Triple;
import ru.ksu.ktk.diploma.sdi.mapping.thesauri.ThesauriDescriptor;
import ru.ksu.ktk.diploma.sdi.util.DOMUtil;

@ContextConfiguration(locations = {"classpath:META-INF/spring/mapping-beans.xml", "classpath:test-dao-beans.xml",
    "classpath:test-properties-beans.xml", "classpath:META-INF/spring/analyzer-beans.xml"})
public class MappingGeneratorTestCase extends AbstractTestNGSpringContextTests {
    @Autowired
    ResourcesDao resourcesDao;
    @Autowired
    MappingGenerator mappingGenerator;

    //    @Test
    public void testWithDescriptors() {
        String corrUri = "http://localhost:8080/exist/rest/db/mom/data/vrm/mappings/corrs_out.xml";
        Document corrDocument = resourcesDao.getDOM(corrUri);
        List<Correspondence> correspondences = DOMUtil.convertFromDOM(corrDocument);
        List<Triple> triples = mappingGenerator.generateMapping(correspondences);
        System.out.print("\n");
        for (Triple triple : triples){
            List<ThesauriDescriptor> descriptors = triple.getDescriptors();
            StringBuilder sb = new StringBuilder();
            for (ThesauriDescriptor descr : descriptors){
                sb.append(descr.toString());
                sb.append(",");
            }
            String descrStr = sb.toString();
            System.out.println(String.format("%s [%s]", triple, descrStr));
        }
    }

    @Test
    public void testGenerateMapping() {
        String corrUri = "http://localhost:8080/exist/rest/db/mom/data/vrm/mappings/corrs_out.xml";
        Document corrDocument = resourcesDao.getDOM(corrUri);
        List<Correspondence> correspondences = DOMUtil.convertFromDOM(corrDocument);
        List<Triple> triples = mappingGenerator.generateMapping(correspondences);
        System.out.print("\n");
        for (Triple triple : triples){
            System.out.println(triple);
        }
    }
}
